<template>
  <div>
    <div>
      <h2>管理员----修改投资项目</h2>
      <table class="table table-borderd">
          <tr>
            <td align="right">投资名称：</td>
            <td><input type="text" v-model="state.I_Name"/></td>
          </tr>
          <tr>
            <td align="right">投资总金额:</td>
            <td><input type="text" v-model="state.I_Budget"/></td>
          </tr>
          <tr>
            <td></td>
            <td><input type="button" value="确认修改" @click="ok"/></td>
          </tr>
      </table>
  </div>
  </div>
</template>

<script setup lang="ts">
import { useRoute,useRouter } from 'vue-router'
import { onMounted,reactive,toRefs } from 'vue'
import axios from 'axios';
//接受参数
const route = useRoute()
const router = useRouter()


const data = reactive({
  state:{
    I_Name:'',
    I_Budget:''
  }
})

let { state } = toRefs(data)


onMounted(()=>{
  getData()
})


//确认修改
const ok = ()=>{
  //1.非空判断
  if(state.value.I_Name == ""){
    alert("投资名称不能为空");
    return;
  }
  if(state.value.I_Budget == ""){
    alert("投资总金额不能为空");
    return;
  }

  if(isNaN(Number(state.value.I_Budget)) == true){
    alert("投资总金额只能为数字不能为空");
    return;
  }
  if(Number(state.value.I_Budget) < 100000 || (Number(state.value.I_Budget) > 1000000 )){
    alert("投资总金额应该在100000-1000000之间");
    return;
  }
  //2.发送网络请求
  axios({
    url:'/api/Invest/PostUpdateInvest',
    method:"post",
    data:state.value
  })
  .then(res=>{
    console.log(res);
    if(res.data.data > 0){
      alert("修改成功");
      router.push("/main/adminInvestList")
    }else if(res.data.data == -1){
      alert("有人投资不能修改");
      router.push("/main/adminInvestList")
      return;
    }else{
      alert("修改失败");
      return;
    }
    
  })
  .catch(err=>{
    console.log(err);
    
  })
}



//反填数据
const getData = ()=>{
  axios({
    url:'/api/Invest/GetInvestItem',
    method:'get',
    params:{
      Iid:route.query.id
    }
  })
  .then(res=>{
    console.log(res);
    state.value = res.data.data
    
  })
  .catch(err=>{
    console.log(err);
    
  })
}

</script>

<style scoped>

</style>